package LeetCode;

import java.math.BigInteger;
import java.util.Arrays;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/21.
//
//https://leetcode.cn/problems/plus-one/description/
public class 加一 {
    public static void main(String[] args) {
        int[] digits = {9,9};
        System.out.println(Arrays.toString(plusOne(digits)));
    }

    private static int[] plusOne(int[] digits) {
        digits[digits.length-1]++;
        for (int i = digits.length-1; i > 0; i--) {
            if(digits[i] == 10){
                digits[i] = 0;
                digits[i-1]++;
            }
        }
        if(digits[0] == 10){
            digits[0] = 0;
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            System.arraycopy(digits,0,res,1,digits.length);
            return res;
        }
        return digits;
    }
//    public static int[] plusOne(int[] digits) {
//        StringBuilder sb = new StringBuilder();
//        for (int digit : digits) {
//            sb.append(digit);
//        }
//        BigInteger b = new BigInteger(sb.toString());
//        b = b.add(new BigInteger("1"));
//        String temp = b.toString();
//        int[] res = new int[temp.length()];
//        for (int i = 0; i < temp.length(); i++) {
//            res[i] = Character.getNumericValue(temp.charAt(i));
//        }
//        return res;
//    }
}
